home *** CD-ROM | disk | FTP | other *** search
- Path: news.ifm.liu.se!d93ricka
- From: d93ricka@isy.liu.se (Richard Karlsson)
- Newsgroups: comp.lang.c
- Subject: Help! Can't wait for two files with aiowait.
- Date: 6 Feb 1996 17:03:52 GMT
- Message-ID: <4f81lo$not@newsy.ifm.liu.se>
- NNTP-Posting-Host: idun.isy.liu.se
- Mime-Version: 1.0
- Content-Type: text/plain; charset=iso-8859-1
- Content-Transfer-Encoding: 8bit
- Summary: Aiowait does not return even though outstanding requesta are finished
- Keywords: aiowait, aioread, asynchronous
- X-Newsreader: NN version 6.5.0 #26 (NOV)
-
- Hello
-
- I'm trying to wait for more than one file at the same time, with the
- aiowait call under SunOS or Solaris. The code below illustrates what
- I'm trying to do. My problem is that aiowait does not return even
- though one of the outstaning ioread-requests have finished.
-
- The program will try to read from "testfil1" and "testfil2" and printf
- the result. Do "mkfifo testfil1", "mkfifo testfil2" and open 3
- shells. In shell 1 do "cat >>testfil1". In shell 2 do "cat
- >>testfil2". And in shell 3 do "testaiowait". Now write
- "111111111111111111111111111111111111" into shell 1 and press return
- in shell 2 a few times. You will notice that only 4 "1":s are read
- from shell 1 for every return you press in shell 2.
-
- Please help me understan why.
-
- (Remembler to link with aio (option -laio to the linker (compiler)))
-
- // testaiowait.c
-
- #include <stdio.h>
- #include <sys/asynch.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <unistd.h>
-
- #define RETURNCODE_FATAL 20
- #define RETURNCODE_OK 0
-
- #define TRUE 1
- #define FALSE 0
-
- main()
- {
- FILE *fil1, *fil2;
- aio_result_t aioresult1, aioresult2, *aioresult;
- char buffer1[1000], buffer2[1000];
- int res1free=TRUE, res2free=1;
- int eof1=FALSE, eof2=FALSE;
-
- if((fil1=fopen("testfil1","r"))==0)
- {
- perror("Can't open testfil1");
- exit(RETURNCODE_FATAL);
- }
-
- if((fil2=fopen("testfil2","r"))==0)
- {
- perror("Can't open testfil2");
- exit(RETURNCODE_FATAL);
- }
-
- do
- {
- if((res1free)&&!eof1) /* If aio_result free and not at eof do another read */
- {
- aioresult1.aio_errno=AIO_INPROGRESS;
- res1free=FALSE;
- if(aioread(fileno(fil1), buffer1, 1, 0, SEEK_END, &aioresult1)==-1)
- {
- perror("Can't aioread on testfil1");
- exit(RETURNCODE_FATAL);
- }
- }
-
- if(res2free&&!eof2) /* If aio_result free and not at eof do another read */
- {
- aioresult2.aio_errno=AIO_INPROGRESS;
- res2free=FALSE;
- if(aioread(fileno(fil2), buffer2, 1, 0, SEEK_END, &aioresult2)==-1)
- {
- perror("Can't aioread on testfil2");
- exit(RETURNCODE_FATAL);
- }
- }
-
- if((int)(aioresult=aiowait(0))==-1) /* aiowait */
- {
- perror("Can't aiowait");
- exit(RETURNCODE_FATAL);
- };
-
- if(aioresult==&aioresult1) /* if read on testfil1 was finished do... */
- {
- res1free=TRUE;
- if(aioresult->aio_return==0)
- {
- eof1=TRUE;
- }
- else
- {
- /* This is necessary if file is seekable.
- if(lseek(fileno(fil1), 1, SEEK_CUR)==-1)
- {
- perror("Can't seek on testfil1");
- exit(RETURNCODE_FATAL);
- }
- */
- buffer1[1]='\0';
- printf("%s",buffer1);
- }
- }
- if(aioresult==&aioresult2) /* if read on testfil2 was finished do... */
- {
- res2free=TRUE;
- if(aioresult->aio_return==0)
- {
- eof2=TRUE;
- }
- else
- {
- /* This is necessary if file is seekable.
- if(lseek(fileno(fil2), 1 ,SEEK_CUR)==-1)
- {
- perror("Can't seek on testfil2");
- exit(RETURNCODE_FATAL);
- }
- */
- buffer2[1]='\0';
- printf("%s",buffer2);
- }
- }
- } while(!eof1||!eof2);
-
- exit(RETURNCODE_OK);
- }
-
- --
-
- /-------------------------------------------------------------------------+
- | Richard Karlsson | Rydsvagen 252 C.23 |
- | Studying Computer Science and Engineering | S-582 51 Linkoping / Sweden |
-